package top.doe.spark.netty

import top.doe.spark.rpc.RpcEndpoint
import top.doe.spark.util.RpcAddress

import java.util

//投递到收件箱的消息
sealed trait InboxMessage

//Inbox在实例化的时候会给自己发送的消息，就是实现了生命周期方法
case object OnStart extends InboxMessage

//发送到Inbox中的异步消息
case class OneWayMessage(
  senderAddress: RpcAddress,
  content: Any
) extends InboxMessage


class Inbox(val endpointName: String, val endpoint: RpcEndpoint) {

  inbox =>

  private val messages = new util.LinkedList[InboxMessage]()

  inbox.synchronized {
    messages.add(OnStart)
  }

  def process(dispatcher: Dispatcher): Unit = {
    var message: InboxMessage = null
    inbox.synchronized {
      message = messages.poll()
      if (message == null) {
        return
      }
    }
    //while (true) {

    //对消息进行模式匹配
    message match {
      case OnStart =>
        //调用对应的RpcEndpoint的生命周期方法
        endpoint.onStart()

      //异步的消息

      //同步的消息
    }


    //}


  }

  //自己给自己发OnStart


}
